package cn.iocoder.yudao.module.game.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
// 解密
public class AESDecrypt {
    public static String decrypt(String data, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"),
            new IvParameterSpec(iv.getBytes()));
        String decryptData = new String(cipher.doFinal(Base64.decodeBase64(data)));
        return decryptData;
    }

    // Sample
    public static void main(String[] args) throws Exception {
        String encryptData = "h2d_GFLFPfFOX8-sWjuRPgDMMbS6mLg9UkxPke8H7s8mMSiSVWBtyd6pgsL4WZCFh01Dt6MFvaggjA45-W2DzlgtvjOvc1bWHB9sO_1MErZfXnONq1M_OTW7Mt72HyduIXeaWCE_dEZEmtxGAMCmkNSVK9t26RLttt2-WwcwjxqsVCVcOIFygfqwPKJxlXV7";
        String key = "96b70e3b0285e619"; // $ { KEY }
        String iv = "c9bc03d6268f1379"; // $ { IV }
        System.out.println(decrypt(encryptData, key, iv)); // return “SampleData”
    }
}